<template>
  <md-popup v-model="show" position="bottom">
    <md-popup-title-bar only-close :title="$t('dao.btn') + form.name" title-align="left" @cancel="show = false" />
    <div style="background: #fff; padding: 20px">
      <md-input-item v-model="form.num" type="money" :placeholder="$t('dao.form.num')" align="center" is-highlight>
        <template #right>
          HWD
        </template>
      </md-input-item>
      <div style="padding-top: 10px; display: flex; justify-content: space-evenly;">
        <md-radio-box v-for="item in list" v-model="form.system_id" :name="item.key" :label="item.name" style="font-size: 12px" />
      </div>
      <div style="padding-top: 25px">
        <md-button type="primary" :loading="loading" @click="action" round>{{ $t('confirm') }}</md-button>
      </div>
    </div>
  </md-popup>
</template>

<script setup>
import { ref } from 'vue'
import { toast } from '@/style'
import { t } from '@/lang'
import { packager, random } from '@/web3'
import { post, system } from '@/api/dao'

const emit = defineEmits(['update'])

const show = ref(false)
const loading = ref(false)
const list = ref([])
const form = ref({
  pledge_id: 0,
  system_id: 0,
  num: '',
  name: ''
})

const open = (data) => {
  show.value = true
  form.value.pledge_id = data.id
  form.value.name = data.name
  form.value.num = ''
}

const action = () => {
  // 打包数据
  loading.value = true
  post(form.value).then(res => {
    toast(t('ok'))
    show.value = false
    emit('update')
  }).catch(err => {
    toast(err.message)
  }).finally(() => {
    loading.value = false
  })
}

system().then(res => {
  list.value = res
  form.value.system_id = res[0].key
})

defineExpose({ open })

</script>